package com.qire.common.widget.animation;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;

public class SimpleAnimationUtil {
    private static final String TAG = SimpleAnimationUtil.class.getSimpleName();

    /**
     * 从控件所在位置移动到控件的底部
     *
     * @return 返回移动的动画对象
     */
    public static TranslateAnimation moveToViewBottom() {
        return moveToViewBottom(500,null);
    }

    /***
     * 从控件所在位置移动到控件的底部动画，允许指定时间及结束事件监听
     * @param durationMillis 持续时间
     * @param endListener   结束监听回调
     * @return
     */
    public static TranslateAnimation moveToViewBottom(long durationMillis, final AnimationEndListener endListener) {
        TranslateAnimation mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
        mHiddenAction.setDuration(durationMillis);
        mHiddenAction.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                if(endListener!=null)
                    endListener.onAnimationEnd(animation);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}
        });
        return mHiddenAction;
    }

    /**
     * 从控件的底部移动到控件所在位置
     *
     * @return
     */
    public static TranslateAnimation moveToViewLocation() {
        return moveToViewLocation(500,null);
    }

    /**
     * 从控件的底部移动到控件所在位置并允许监听
     * @param durationMillis 持续时间
     * @param endListener   结束监听回调
     * @return
     */
    public static TranslateAnimation moveToViewLocation(long durationMillis, final AnimationEndListener endListener) {
        TranslateAnimation mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        mShowAction.setDuration(durationMillis);
        mShowAction.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                if(endListener!=null)
                    endListener.onAnimationEnd(animation);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}
        });
        return mShowAction;
    }

    /**
     * 在一个View之上播放指定的动画
     *
     * @param context  Content上下文
     * @param animId   指定动画ID
     * @param playView 准备播放动画的View
     * @return
     */
    public static Animation playAnimationOnView(Context context, int animId, View playView) {
        return playAnimationOnView(context, animId, playView, null);
    }

    /**
     * 在一个View之上播放指定的动画，并且允许添加一个结束回调函数。
     * @param context       Content上下文
     * @param animId        指定动画ID
     * @param playView      准备播放动画的View
     * @param endListener   播放完毕后的回调
     * @return
     */
    public static Animation playAnimationOnView(Context context, int animId, View playView, final AnimationEndListener endListener){
        Animation animation = AnimationUtils.loadAnimation(context,animId);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                if(endListener!=null)
                    endListener.onAnimationEnd(animation);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}
        });
        playView.startAnimation(animation);
        return animation;
    }

    public interface AnimationEndListener{
        void onAnimationEnd(Animation animation);
    }

}
